package com.uncopt.ohos.widget.text.justify;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.render.Canvas;
import ohos.agp.text.Font;
import ohos.agp.text.RichText;
import ohos.agp.text.RichTextBuilder;
import ohos.agp.text.TextForm;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class JustifiedTextView extends Text implements Component.DrawTask {
    public boolean goNext = true;

    private List<RichTextBean> mRichTextList = new ArrayList<>();
    private Font mFont;
    private RichTextTouchListener mTouchListener;

    @SuppressWarnings("unused")
    public JustifiedTextView(@NotNull Context context) {
        super(context);
        init(context);
    }

    @SuppressWarnings("unused")
    public JustifiedTextView(@NotNull Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context);
    }

    @SuppressWarnings("unused")
    public JustifiedTextView(@NotNull Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    private void init(Context context) {
        mFont = getFont(context);
        mContext = context;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        RichTextBuilder richTextBuilder = new RichTextBuilder();
        if (mRichTextList.size() > 0) {
            for (int i = 0; i < mRichTextList.size(); i++) {
                System.out.println("******onDraw----" + mRichTextList.get(i).getTextStr().length());
                richTextBuilder.revertForm();
                richTextBuilder.mergeForm(mRichTextList.get(i).getTextForm());
                richTextBuilder.addText(mRichTextList.get(i).getTextStr());
            }
            RichText rich = richTextBuilder.build();
            mTouchListener.onTouch(rich);
            setRichText(rich);
        }
    }

    @SuppressWarnings("unused")
    public void setRichTextData(@NotNull List<RichTextBean> dataList) {
        mRichTextList = dataList;
    }

    public TextForm getTextNormal() {
        TextForm formTex = new TextForm();
        formTex.setTextColor(Color.getIntColor("#000000"));
        formTex.setTextSize(50);
        formTex.setTextFont(mFont);
        return formTex;
    }

    public TextForm getTextBold() {
        TextForm formTex = new TextForm();
        formTex.setTextColor(Color.getIntColor("#000000"));
        formTex.setTextSize(50);
        formTex.setTextFont(getBlodFont(mContext));
        return formTex;
    }

    public TextForm getTextFormURL() {
        TextForm formUrl = new TextForm();
        formUrl.setTextColor(Color.getIntColor("#00ccff"));
        formUrl.setTextSize(50);
        formUrl.setUnderline(true);
        formUrl.setTextFont(mFont);
        return formUrl;
    }

    private Font getFont(Context context) {
        File file = CustomFont.readFontFile(context);
        Font.Builder builder = new Font.Builder(file);
        Font font = builder.build();
        return font;
    }

    private Font getBlodFont(Context context) {
        File file = CustomFont.readFontFile(context);
        Font.Builder builder = new Font.Builder(file);
        builder.setWeight(Font.BOLD);
        Font font = builder.build();
        return font;
    }

    public int getLength(int j) {
        int a = 0;
        for (int i = 0; i <= j; i++) {
            a += mRichTextList.get(i).getTextStr().length();
        }
        return a;
    }

    public interface RichTextTouchListener {
        void onTouch(RichText richText);
    }

    public void setRichTextTouchListener(RichTextTouchListener listener) {
        mTouchListener = listener;
    }
}
